package com.hdjk.view.util;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.hdjk.view.activity.main.LoginActivity;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.config.Appconfig;
import com.hdjk.view.entity.UserEntity;
import com.hdjk.view.network.Network;
import com.lst.projectlib.entity.Msg;
import com.lst.projectlib.util.Preferences;

import java.util.Date;

public class UserTokenUtil {

    public static void userLoginAction(Context context, String token, String exToken, String userName, int holdingCode,String phone,String pwd,String id) {
        Preferences pre = new Preferences(context);
        pre.setToken(token);
        pre.setExToken(exToken);
        pre.setUserName(userName);
        pre.setId(id);
        pre.setHoldingCode(holdingCode);
        pre.setPhone(phone);
        pre.setLoginPwd(pwd);
    }

    public static String getToken(Context context) {
        return new Preferences(context).getToken();
    }

    public static void setToken(Context context, String token) {
        new Preferences(context).setToken(token);
    }

    public static String getExToken(Context context) {
        return new Preferences(context).getExToken();
    }

    public static void setExToken(Context context, String token) {
        new Preferences(context).setExToken(token);
    }

    public static String getUserName(Context context) {
        return new Preferences(context).getUserName();
    }

    public static void setUserName(Context context, String userName) {
        new Preferences(context).setUserName(userName);
    }

    public static String getUserId(Context context) {
        return new Preferences(context).getId();
    }

    public static void setUserId(Context context, String id) {
        new Preferences(context).setId(id);
    }
    public static String getPhone(Context context) {
        return new Preferences(context).getPhone();
    }

    public static void setPhone(Context context, String phone) {
        new Preferences(context).setPhone(phone);
    }
    public static String getLoginPwd(Context context) {
        return new Preferences(context).getLoginPwd();
    }

    public static void setLoginPwd(Context context, String loginPwd) {
        new Preferences(context).setLoginPwd(loginPwd);
    }
    public static int getHoldingCode(Context context) {
        return new Preferences(context).getHoldingCode();
    }

    public static void setHoldingCode(Context context, int holdingCode) {
        new Preferences(context).setHoldingCode(holdingCode);
    }

    public static void setFirstVersion(Context context, String firstVersion) {
        Preferences pre = new Preferences(context);
        pre.setFirstVersion(firstVersion);
    }

    public static String getFirstVersion(Context context) {
        Preferences pre = new Preferences(context);
        return pre.getFirstVersion();
    }

    public static boolean isLogin(Context context) {
        String token = new Preferences(context).getToken();
        if (TextUtils.isEmpty(token)) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean isOpenAccount(Context context) {
        int holdingCode = new Preferences(context).getHoldingCode();
        return holdingCode > 0;
    }

    public static String isFirst(Context context){
        return new Preferences(context).getIsFirst();
    }

    public static void setIsFirst(Context context,String bool){
        new Preferences(context).setIsFirst(bool);
    }

    public static void userQuitAction(final Context context) {
        //清除preference
        Preferences pre = new Preferences(context);
        pre.setToken("");
        pre.setExToken("");
        pre.setUserName("");
        pre.setHoldingCode(0);
//		((Activity) context).runOnUiThread(new Runnable() {
//
//			@Override
//			public void run() {
//				Intent intent = new Intent(context, LoginActivity.class);
//				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//				context.startActivity(intent);
//			}
//		});
    }

    public static  void getNewToken() {
        String userId = UserTokenUtil.getUserId(Appconfig.instance);
        String timeStamp = new Date().getTime() + "";
        String deviceName = "android";
        String exchangeToken = StringAppUtil.getMD5(timeStamp + UserTokenUtil.getExToken(Appconfig.instance));
        Network.getNewToken(userId, timeStamp, deviceName, exchangeToken, new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                if (msg.getData() != null && msg.getData() instanceof UserEntity) {
                    UserEntity entity = (UserEntity) msg.getData();
                    Preferences pre = new Preferences(Appconfig.instance);
                    pre.setToken(entity.getToken());
                    pre.setExToken(StringAppUtil.getMD5(UserTokenUtil.getExToken(Appconfig.instance) + entity.getToken()));
                    pre.setUserName(entity.getLoginName());
                    pre.setHoldingCode(entity.getHoldingCode());
                    pre.setPhone(entity.getPhone());
                }else {
                    ToastUtils.showLongToast(Appconfig.instance, "登录超时，请重新登录");
                    userQuitAction(Appconfig.instance);
                    Intent intent = new Intent(Appconfig.instance, LoginActivity.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Appconfig.instance.startActivity(intent);
                }
            }

            @Override
            public void onCallBackError(Msg msg) {
                ToastUtils.showLongToast(Appconfig.instance, "登录超时，请重新登录");
                userQuitAction(Appconfig.instance);
                Intent intent = new Intent(Appconfig.instance, LoginActivity.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Appconfig.instance.startActivity(intent);
            }
        });
    }
}
